from fastapi import FastAPI, Query
from typing import List, Optional
from datetime import datetime
import uvicorn
from load_adata import *

app = FastAPI(
    title="股票数据API",
    description="基于adata的股票市场数据接口",
    version="1.0.0"
)

@app.get("/api/v1/stocks", tags=["股票基础信息"])
async def list_stocks():
    """
    获取所有股票代码
    
    返回:
        DataFrame: 包含股票代码、简称、交易所、上市日期
    """
    return get_all_stock_code().to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/info", tags=["股票基础信息"])
async def get_stock_basic_info(
    stock_code: str,
    k_type: int = Query(1, description="K线类型：1.日；2.周；3.月"),
    start_date: Optional[str] = Query(None, description="开始日期，格式：YYYY-MM-DD")
):
    """
    获取单只股票的行情信息
    """
    return get_stock_info(stock_code, k_type, start_date).to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/capital", tags=["股票基础信息"])
async def get_stock_capital(stock_code: str):
    """
    获取单只股票的股本信息
    """
    return get_stock_capital_info(stock_code).to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/industry", tags=["股票基础信息"])
async def get_stock_industry(stock_code: str):
    """
    获取单只股票的申万一二级行业
    """
    return get_stock_industry_info(stock_code).to_dict(orient="records")

@app.get("/api/v1/concepts", tags=["概念板块"])
async def list_concepts():
    """
    获取所有A股概念代码信息（同花顺）
    """
    return get_all_concept_info().to_dict(orient="records")

@app.get("/api/v1/trading-calendar", tags=["交易日历"])
async def get_trading_calendar():
    """
    获取股票交易日信息
    """
    return get_stock_trade_date().to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/dividend", tags=["股票信息"])
async def get_stock_dividend(stock_code: str):
    """
    获取单只股票的分红信息
    """
    return get_stock_dividend_info(stock_code).to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/kline", tags=["行情数据"])
async def get_stock_kline(
    stock_code: str,
    k_type: int = Query(1, description="K线类型：1.日；2.周；3.月,4.季度，5.5min，15.15min，30.30min，60.60min"),
    start_date: Optional[str] = Query(None, description="开始日期，格式：YYYY-MM-DD"),
    end_date: Optional[str] = Query(None, description="结束日期，格式：YYYY-MM-DD"),
    adjust_type: int = Query(1, description="复权类型：0.不复权；1.前复权；2.后复权")
):
    """
    获取单只股票的K线数据
    """
    return get_stock_kline_info(stock_code, k_type, start_date, end_date, adjust_type).to_dict(orient="records")

@app.get("/api/v1/stocks/realtime-minutes", tags=["行情数据"])
async def get_stocks_minute_data(stock_codes: List[str] = Query(..., description="股票代码列表")):
    """
    获取多个股票的今日分时行情
    """
    return get_market_min(stock_codes).to_dict(orient="records")

@app.get("/api/v1/stocks/realtime-quotes", tags=["行情数据"])
async def get_stocks_realtime_quotes(stock_codes: List[str] = Query(..., description="股票代码列表")):
    """
    获取多个股票最新行情信息
    """
    return get_market_info(stock_codes).to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/level5", tags=["行情数据"])
async def get_stock_level5(stock_code: str):
    """
    获取单个股票的5档行情信息
    """
    return get_market_tick(stock_code).to_dict(orient="records")

@app.get("/api/v1/stocks/{stock_code}/trades", tags=["行情数据"])
async def get_stock_trades(stock_code: str):
    """
    获取单个股票的分笔成交行情
    """
    return get_market_tick_detail(stock_code).to_dict(orient="records")

@app.get("/api/v1/concepts/{concept_code}/kline", tags=["概念行情"])
async def get_concept_kline(
    concept_code: str,
    k_type: int = Query(1, description="K线类型：1.日；2.周；3.月"),
    adjust_type: int = Query(1, description="复权类型：0.不复权；1.前复权；2.后复权")
):
    """
    获取同花顺概念的K线数据
    """
    return get_concept_kline_info(concept_code, k_type, adjust_type).to_dict(orient="records")

@app.get("/api/v1/concepts/eastmoney/{concept_code}/kline", tags=["概念行情"])
async def get_eastmoney_concept_kline(
    concept_code: str,
    k_type: int = Query(1, description="K线类型：1.日；2.周；3.月")
):
    """
    获取东方财富概念的K线数据
    """
    return get_concept_kline_info_eastmoney(concept_code, k_type).to_dict(orient="records")

@app.get("/api/v1/concepts/{concept_code}/minutes", tags=["概念行情"])
async def get_concept_minutes(concept_code: str):
    """
    获取同花顺概念当日分时数据
    """
    return get_concept_min_info(concept_code).to_dict(orient="records")

@app.get("/api/v1/concepts/{concept_code}/realtime", tags=["概念行情"])
async def get_concept_realtime(concept_code: str):
    """
    获取同花顺概念实时行情
    """
    return get_concept_current_info(concept_code).to_dict(orient="records")

@app.get("/api/v1/indices/{index_code}/kline", tags=["指数行情"])
async def get_index_kline(
    index_code: str,
    k_type: int = Query(1, description="K线类型：1.日；2.周；3.月"),
    start_date: Optional[str] = Query(None, description="开始日期，格式：YYYY-MM-DD")
):
    """
    获取指数K线数据
    """
    return get_index_kline_info(index_code, k_type, start_date).to_dict(orient="records")

@app.get("/api/v1/indices/{index_code}/minutes", tags=["指数行情"])
async def get_index_minutes(index_code: str):
    """
    获取指数当日分时数据
    """
    return get_index_min_info(index_code).to_dict(orient="records")

@app.get("/api/v1/indices/{index_code}/realtime", tags=["指数行情"])
async def get_index_realtime(index_code: str):
    """
    获取指数实时行情
    """
    return get_index_current_info(index_code).to_dict(orient="records")

@app.get("/api/v1/capital-flow/{stock_code}/minutes", tags=["资金流向"])
async def get_stock_capital_flow_minutes(stock_code: str):
    """
    获取单个股票的今日分时资金流向
    """
    return get_capital_flow_min_info(stock_code).to_dict(orient="records")

@app.get("/api/v1/capital-flow/{stock_code}/daily", tags=["资金流向"])
async def get_stock_capital_flow_daily(
    stock_code: str,
    start_date: Optional[str] = Query(None, description="开始日期，格式：YYYY-MM-DD"),
    end_date: Optional[str] = Query(None, description="结束日期，格式：YYYY-MM-DD")
):
    """
    获取单个股票的历史资金流向数据
    """
    return get_capital_flow_info(stock_code, start_date, end_date).to_dict(orient="records")

@app.get("/api/v1/capital-flow/concepts/{concept_code}", tags=["资金流向"])
async def get_concept_capital_flow(concept_code: str):
    """
    获取东方财富概念近1,5,10日资金流向
    """
    return get_all_capital_flow_info(concept_code).to_dict(orient="records")

@app.get("/api/v1/hot-lists/eastmoney", tags=["热度榜单"])
async def get_eastmoney_hot():
    """
    获取东方财富热度榜单
    """
    return get_eastmoney_hot_list().to_dict(orient="records")

@app.get("/api/v1/hot-lists/ths", tags=["热度榜单"])
async def get_ths_hot():
    """
    获取同花顺热度榜单
    """
    return get_ths_hot_list().to_dict(orient="records")

@app.get("/api/v1/hot-lists/ths-concepts", tags=["热度榜单"])
async def get_ths_hot_concepts():
    """
    获取同花顺热门概念板块20排行榜
    """
    return get_ths_hot_concept_list().to_dict(orient="records")

@app.get("/api/v1/longhu/list", tags=["龙虎榜"])
async def get_longhu():
    """
    获取龙虎榜单列表
    """
    return get_longhu_list().to_dict(orient="records")

@app.get("/api/v1/longhu/stocks/{stock_code}", tags=["龙虎榜"])
async def get_stock_longhu(stock_code: str):
    """
    获取单只股票龙虎榜信息详情
    """
    return get_stock_longhu_info(stock_code).to_dict(orient="records")

if __name__ == "__main__":
    uvicorn.run("api_server:app", host="0.0.0.0", port=8000, reload=True) 